package com.aiwu.memory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import org.citra.citra_emu.R;

/**
 * 内存查看器
 */
public final class MemoryDialogActivity extends FragmentActivity implements View.OnClickListener, MemoryActView {
    private MemoryFirstFragment memoryFirstFragment;
    private MemoryLoadingFragment memoryLoadingFragment;
    private MemoryResultManyFragment memoryResultManyFragment;
    private MemoryResultListFragment memoryResultListFragment;
    private MemoryNormalBackGameFragment memoryBackGameFragment;
    private MemoryFuzzyBackGameFragment memoryFuzzyBackGameFragment;
    private KeyboardFragment keyboardFragment;

    public static void launch(Context context) {
        Intent intent = new Intent(context, MemoryDialogActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_dialog);
        initView();
        setAttributes();
        MemoryManager.getInstance().setMemoryActView(this);
        restoreStep();
    }

    @Override
    protected void onDestroy() {
        MemoryManager.getInstance().removeMemoryActView();
        super.onDestroy();
    }

    private void restoreStep() {
        MemoryManager.getInstance().restoreStep();
    }

    @Override
    public void showFirstFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryFirstFragment())
                .show(getMemoryFirstFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showLoadingFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryLoadingFragment())
                .show(getMemoryFirstFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showResultManyFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryResultManyFragment())
                .show(getMemoryResultManyFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showResultListFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryResultListFragment())
                .show(getMemoryResultListFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showNormalBackGameFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getNormalBackGameFragment())
                .show(getNormalBackGameFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showFuzzyBackGameFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getFuzzyBackGameFragment())
                .show(getFuzzyBackGameFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showKeyboardFragment(boolean search, MemoryManager.KeyBoardOffsetCallback keyBoardOffsetCallback) {
        Bundle arg = new Bundle();
        arg.putBoolean(KeyboardFragment.EXTRA_SEARCH, search);
        getKeyboardFragment().setArguments(arg);
        FrameLayout layoutKeyboard = findViewById(R.id.layout_fragment_keyboard);
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) layoutKeyboard.getLayoutParams();
        if (keyBoardOffsetCallback.setKeyBoardOffset(lp)) {
            layoutKeyboard.setLayoutParams(lp);
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_keyboard, getKeyboardFragment())
                .show(getKeyboardFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void hideKeyboardFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.hide(getKeyboardFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void finishAct() {
        finish();
    }

    @Override
    public KeyboardFragment getKeyboardFragment() {
        if (keyboardFragment == null) {
            keyboardFragment = new KeyboardFragment();
        }
        return keyboardFragment;
    }

    @Override
    public MemoryLoadingFragment getMemoryLoadingFragment() {
        if (memoryLoadingFragment == null) {
            memoryLoadingFragment = new MemoryLoadingFragment();
        }
        return memoryLoadingFragment;
    }

    @Override
    public MemoryResultManyFragment getMemoryResultManyFragment() {
        if (memoryResultManyFragment == null) {
            memoryResultManyFragment = new MemoryResultManyFragment();
        }
        return memoryResultManyFragment;
    }

    @Override
    public MemoryResultListFragment getMemoryResultListFragment() {
        if (memoryResultListFragment == null) {
            memoryResultListFragment = new MemoryResultListFragment();
        }
        return memoryResultListFragment;
    }

    @Override
    public MemoryFirstFragment getMemoryFirstFragment() {
        if (memoryFirstFragment == null) {
            memoryFirstFragment = new MemoryFirstFragment();
        }
        return memoryFirstFragment;
    }

    @Override
    public MemoryNormalBackGameFragment getNormalBackGameFragment() {
        if (memoryBackGameFragment == null) {
            memoryBackGameFragment = new MemoryNormalBackGameFragment();
        }
        return memoryBackGameFragment;
    }

    @Override
    public MemoryFuzzyBackGameFragment getFuzzyBackGameFragment() {
        if (memoryFuzzyBackGameFragment == null) {
            memoryFuzzyBackGameFragment = new MemoryFuzzyBackGameFragment();
        }
        return memoryFuzzyBackGameFragment;
    }

    private void initView() {
        findViewById(R.id.btn_reset).setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_reset) {
            MemoryManager.getInstance().reset();
        } else if (v.getId() == R.id.btn_close) {
            finish();
        }
    }

    private void setAttributes() {
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);
    }
}
